class Solution {
public:
  bool searchMatrix(vector<vector<int>>& matrix, int target) {

    bool f = false;
    int m = matrix.size();
    int n = matrix.at(0).size();
    pair<int, int> index(m - 1, 0);

    while(0 <= index.first && index.first <= m - 1 && 0 <= index.second && index.second <= n - 1){
      int temp = matrix.at(index.first).at(index.second);
      if(temp == target){
        f = true;
        break;
      }else if(temp > target){
        --index.first;
      }else{
        ++index.second;
      }
    }

    return f;

  }
};